const path = require('path')

const sass = require('node-sass')
const loadTasks = require('load-grunt-tasks')

module.exports = grunt => {

    const data = {
        menus: [{
            name: 'Home',
            icon: 'aperture',
            link: 'index.html'
        },
        {
            name: 'Features',
            link: 'features.html'
        },
        {
            name: 'About',
            link: 'about.html'
        },
        {
            name: 'Contact',
            link: '#',
            children: [{
                name: 'Twitter',
                link: 'https://twitter.com/w_zce'
            },
            {
                name: 'About',
                link: 'https://weibo.com/zceme'
            },
            {
                name: 'divider'
            },
            {
                name: 'About',
                link: 'https://github.com/zce'
            }
            ]
        }
        ],
        pkg: require('./package.json'),
        date: new Date()
    }

    const opts = {
        dist: 'dist/',
        src: './src',
        public: 'public/',
        styles: 'assets/styles/**/*.scss',
        scripts: 'assets/scripts/**/*.js',
        pages: '*.html',
        images: 'assets/images/**/*',
        fonts: 'assets/fonts/**/*',
    }

    grunt.initConfig({
        clean: {
            dist: {
                force: true,
                src: [opts.dist]
            }
        },

        // 样式文件
        sass: {
            options: {
                implementation: sass,
                sourceMap: true
            },
            main: {
                files: [{
                    expand: true,
                    cwd: opts.src,
                    src: [opts.styles],
                    dest: opts.dist,
                    ext: '.css'
                }]
            }
        },

        // 脚本文件
        babel: {
            options: {
                sourceMap: true,
                presets: ['@babel/preset-env']
            },
            main: {
                files: [{
                    expand: true,
                    cwd: opts.src,
                    src: [opts.scripts],
                    dest: opts.dist
                }]
            }
        },

        // 页面文件
        web_swig: {
            options: {
                swigOptions: {
                    cache: false
                },
                getData: data
            },
            main: {
                files: [{
                    expand: true,
                    cwd: opts.src,
                    src: [opts.pages],
                    dest: opts.dist
                }]
            }
        },

        // 图片压缩
        imagemin: {
            dist: {
                options: {
                    optimizationLevel: 3, //定义 PNG 图片优化水平
                    svgoPlugins: [{ removeViewBox: false }],
                    // use: [mozjpeg()]
                },
                files: [{
                    expand: true,
                    cwd: opts.src,
                    src: [opts.images], // 优化 img 目录下所有 png/jpg/jpeg 图片
                    dest: opts.dist // 优化后的图片保存位置，覆盖旧图片，并且不作提示
                }]
            }
        },

        // 脚本压缩
        uglify: {
            options: {
            },
            assets: {
                files: [{
                    expand: true,
                    cwd: opts.dist,
                    src: ['**/*.js'],
                    dest: opts.dist
                }]
            },
        },

        // 样式压缩
        cssmin: {
            combine: {
                files: [{
                    expand: true,
                    cwd: opts.dist,
                    src: ['**/*.css'],
                    dest: opts.dist
                }]
            }
        },

        htmlmin: {
            dist: {
                options: {
                    removeComments: true,
                    collapseWhitespace: true,
                    minifyCSS: true,
                    minifyJS: true,
                },
                files: [{
                    expand: true,
                    cwd: opts.dist,
                    src: ['**/*.html'],
                    dest: opts.dist
                }]
            },
        },

        // 复制其它资源
        copy: {
            assets: {
                files: [{
                    expand: true,
                    cwd: opts.public,
                    src: ['**/*'],
                    dest: opts.dist
                }]
            },

            images: {
                files: [{
                    expand: true,
                    cwd: opts.src,
                    src: [opts.images],
                    dest: opts.dist
                }]
            },

            fonts: {
                files: [{
                    expand: true,
                    cwd: opts.src,
                    src: [opts.fonts],
                    dest: opts.dist
                }]
            },
        },

        watch: {
            files: [path.join(opts.src, '**/*')],
            tasks: ['compile']
        },

        // web服务
        browserSync: {
            dev: {
                bsFiles: {
                    src: [
                        path.join(opts.dist, '**/*')
                    ]
                },
                options: {
                    watchTask: true,
                    open: false,
                    server: {
                        baseDir: ['dist', 'src', 'public'],
                        routes: {
                            '/node_modules': 'node_modules'
                        }
                    }
                }
            }
        }
    })


    // 加载所有插件
    loadTasks(grunt)

    grunt.registerTask('compile', ['sass', 'babel', 'web_swig'])

    grunt.registerTask('build', ['clean', 'sass', 'babel', 'web_swig', 'uglify', 'cssmin', 'htmlmin', 'copy']) // 'imagemin', 报错

    grunt.registerTask('develop', ['clean', 'compile', 'browserSync', 'watch'])

    grunt.registerTask('default', ['build'])
}